const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    next();
});
//所有请求过来，都去项目当前的images目录下寻找所请求的文件，找到就返回
app.use(express.static('../images/product'));
//选择diskStorage存储
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, path.resolve('../images/product'));
    },

    filename: function (req, file, cb) {
        cb(null, Date.now() + path.extname(file.originalname));//增加了文件的扩展名
    }
});
const upload = multer({ storage: storage });
//图片上传地址
app.post('/profile', upload.single('avatar'), function (req, res, next) {
    res.send({
        err: null,
        //filePath:就是图片在项目中的存放路径
        filePath: path.basename(req.file.path)
    });
});
//3001端口
app.listen(3001, function () {
    console.log("端口 3001 开启成功");

});